package org.hit.burkun.cases.pipline;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.hit.burkun.db.DBHelper;
import org.hit.burkun.db.DBTypeConverter;
import org.hit.burkun.dbimport.FileBaseImport;
import org.hit.burkun.file.FileProcessHelper.FILE_TYPE;
import org.hit.burkun.mapping.IOnlineMappingProcessor.DBKEY;

public class StringInteractionImport extends FileBaseImport {

	public StringInteractionImport(String fullFileName, FILE_TYPE fileType,
			String tableName, DBHelper dbHelper, int totalPropertyNum,
			boolean hasAutoIncreasementID, DBTypeConverter typeConverter,
			LINE_SPLITER spliterType) {
		super(fullFileName, fileType, tableName, dbHelper, totalPropertyNum,
				hasAutoIncreasementID, typeConverter, spliterType);
	}

	@Override
	public String[] forEachItem(String[] item) {
		// pid, protein1 protein2 neighborhood fusion cooccurence coexpression
		// experimental database textmining combined_score source
		// id geneid geneid stringid stringdi score source
		// 固定source
		item[item.length - 1] = "string";
		if (map.containsKey(item[1]) && map.containsKey(item[2])) {
			// 如果包含geneid
			Set<String> values1 = map.get(item[1]);
			Set<String> values2 = map.get(item[2]);
			double nScore = Integer.parseInt(item[3]) / 1000.0;
			double fSocre = Integer.parseInt(item[4]) / 1000.0;
			double coocScore = Integer.parseInt(item[5]) / 1000.0;
			double coExpressScore = Integer.parseInt(item[6]) / 1000.0;
			double experScore = Integer.parseInt(item[7]) / 1000.0;
			double databaseScore = Integer.parseInt(item[8]) / 1000.0;
			double textScore = Integer.parseInt(item[9]) / 1000.0;
			double combScore = Integer.parseInt(item[10]) / 1000.0;
			item[3] = item[1];
			item[4] = item[2];
			item[1] = StringUtils.join(values1, ",");
			item[2] = StringUtils.join(values2, ",");
			item[5] = String.valueOf(nScore);
			item[6] = String.valueOf(fSocre);
			item[7] = String.valueOf(coocScore);
			item[8] = String.valueOf(coExpressScore);
			item[9] = String.valueOf(experScore);
			item[10] = String.valueOf(databaseScore);
			item[11] = String.valueOf(textScore);
			item[12] = String.valueOf(combScore);
		} else {
			return null;
		}
		return item;
	}

	public static Map<String, Set<String>> map = new HashMap<String, Set<String>>();
	private static Map<String, String> bioInteractionMap = new HashMap<String, String>();

	static {
		DBHelper dbh = DBHelper.getLocalBioSearch();
		String sqlStr = "select dbkey1, dbkey2 from dbkey_mapping where dbkey1_type = '"
				+ DBKEY.GeneID.getTag()
				+ "' and dbkey2_type = '"
				+ DBKEY.STRING.getTag() + "'";
		System.out.println(sqlStr);
		dbh.connectionDB();
		Collection<String[]> res = dbh.queryData(sqlStr, 2);
		dbh.disconnection();
		for (String[] line : res) {
			if (map.containsKey(line[1])) {
				map.get(line[1]).add(line[0]);
			} else {
				HashSet<String> set = new HashSet<String>();
				set.add(line[0]);
				map.put(line[1], set);
			}
		}
		// biograph
		dbh = DBHelper.getLocalBioGraph();
		sqlStr = "SELECT distinct `BioGRID Interaction ID`, `Entrez Gene Interactor A`, `Entrez Gene Interactor B` FROM biograph.biogrid_intercation_human";
		dbh.connectionDB();
		res = dbh.queryData(sqlStr, 3);
		dbh.disconnection();
		for (String[] line : res) {
			String key = line[1] + "-" + line[2];
			bioInteractionMap.put(key, line[0]);
		}
	}

}
